<?php
require_once 'modules/event/const.inc';
class Helper_Event extends Zend_Controller_Action_Helper_Abstract
{
	private $userEventCounts = array();
	private $_prefix;
	private $_ctrl;
	
	public function init()
    {
        $this->_prefix = EVENT_CACHE_PREFIX;
        $this->_ctrl = $this->getActionController();
    }
	
	private function getOtherHelper($helperName){
		return Zend_Controller_Action_HelperBroker::getStaticHelper($helperName);
	}
	
	public function getEventInfo($eid){
		Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/modules/event/helpers', 'Helper');
		$modelLoaderHelper = $this->getOtherHelper('modelLoader');
		$modelLoaderHelper->setCtrl($this->_ctrl);
		return $modelLoaderHelper->getEventInfo($eid);
	}
	
	public function getEventCategories(){
		$key = 'event_event_categories';
        $categories = $this->_ctrl->getCache($key, $this->_prefix);
        if ( $categories === false){
	        $termModel = AppGlobal::loadModel('Term', 'event');
	        $categories = $termModel->getTermOfEvent();
	        $this->_ctrl->setCache($key, $categories, $this->_prefix);
        }
        return $categories;
	}
	
    public function getUserEvents($uid, $page=0, $limit=20, $timeInx = "all"){
    	
    	$key = 'event_user_events_' . $uid."_".$page."_".$timeInx;
        $events = $this->_ctrl->getCache($key, $this->_prefix);
        if ( $events === false){
	        $myModel = AppGlobal::loadModel('My', 'event');
	        $events = $myModel->getMyEvent($uid, $timeInx, $page);
	        $this->_ctrl->setCache($key, $events, $this->_prefix);
        }
        return $events;
    }
    
    
    public function getUserEventCount($uid, $timeInx = "all"){
    	$key = 'event_user_event_count_' . $uid."_".$timeInx;
        $total = $this->_ctrl->getCache($key, $this->_prefix);
        if ($total === FALSE){
	    	$eventModel = AppGlobal::loadModel('My', 'event');
	        $total = $eventModel->getCountMyEvent($uid, $timeInx);
	        $this->_ctrl->setCache($key, $total, $this->_prefix);
        }
        return $total;
    }
    
	public function getUserOwnEvents($uid, $page=0, $limit=20, $timeInx = "all"){
    	
    	$key = 'event_user_own_events_' . $uid."_".$page."_".$timeInx;
        $events = $this->_ctrl->getCache($key, $this->_prefix);
        if ( $events === false){
	        $myModel = AppGlobal::loadModel('My', 'event');
	        $events = $myModel->getMyOwnEvent($uid, $timeInx, $page);
	        $this->_ctrl->setCache($key, $events, $this->_prefix);
        }
        return $events;
    }
    
    
    public function getUserOwnEventCount($uid, $timeInx = "all"){
    	$key = 'event_user_own_event_count_' . $uid."_".$timeInx;
        $total = $this->_ctrl->getCache($key, $this->_prefix);
        if ($total === FALSE){
	    	$eventModel = AppGlobal::loadModel('My', 'event');
	        $total = $eventModel->getCountMyOwnEvent($uid, $timeInx);
	        $this->_ctrl->setCache($key, $total, $this->_prefix);
        }
        return $total;
    }
    
	public function getUserFriendEvents($uid, $fid, $page=0, $limit=20, $timeInx = "all"){
    	
    	$key = 'event_user_events_'  .(is_numeric($fid) && $fid>0?$fid:$uid."_0")."_".$page."_".$timeInx;
        $events = $this->_ctrl->getCache($key, $this->_prefix);
        if ( $events === false){
	        $myModel = AppGlobal::loadModel('My', 'event');
	        $events = $myModel->getMyFriendEvent($uid, $fid, $timeInx, $page);
	        $this->_ctrl->setCache($key, $events, $this->_prefix);
        }
        return $events;
    }
    
    
    public function getUserFriendEventCount($uid, $fid, $timeInx = "all"){
    	$key = 'event_user_event_count_' .(is_numeric($fid) && $fid>0?$fid:$uid."_0")."_".$timeInx;
        $total = $this->_ctrl->getCache($key, $this->_prefix);
        if ($total === FALSE){
	    	$eventModel = AppGlobal::loadModel('My', 'event');
	        $total = $eventModel->getCountMyFriendEvent($uid, $fid, $timeInx);
	        $this->_ctrl->setCache($key, $total, $this->_prefix);
        }
        return $total;
    }
    
    public function getNetworkEvent($nwid, $page=0, $limit=20){
    	$key = 'event_network_events_' . $nwid."_".$page;
        $events = $this->_ctrl->getCache($key, $this->_prefix);
        if ( $events === false){
	    	$eventModel = AppGlobal::loadModel('Event', 'event');
	        $events = $eventModel->getNetworkEvent($nwid, $page, $limit);
	        $this->_ctrl->setCache($key, $events, $this->_prefix);
        }
        return $events;
    }
    
	public function getNetworkEventCount($nwid){
		$key = 'event_network_event_count_' . $nwid;
        $total = $this->_ctrl->getCache($key, $this->_prefix);        
        if ($total === FALSE){
	    	$eventModel = AppGlobal::loadModel('Event', 'event');
	        $total = $eventModel->getNetworkEventCount($nwid);
	        $this->_ctrl->setCache($key, $total, $this->_prefix);
        }
        return $total;
    }
    
	public function isCanAdd($type, $eid, $uid){
		
		Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/modules/event/helpers', 'Helper');
		$privacyHelper = $this->getOtherHelper('privacy');
		$privacyHelper->setCtrl($this->_ctrl);
		$isCanAdd = $privacyHelper->isCanAdd($type, $eid, $uid);
        return $isCanAdd;
	}
	
	public function isEnable($type, $eid){
		
		Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/modules/event/helpers', 'Helper');
		$privacyHelper = $this->getOtherHelper('privacy');
		$privacyHelper->setCtrl($this->_ctrl);
		$isCanAdd = $privacyHelper->isEnable($type, $eid);
        return $isCanAdd;
	}
	
	public function isAdmin($eid, $uid){
		
		Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/modules/event/helpers', 'Helper');
		$privacyHelper = $this->getOtherHelper('modelLoader');
		$privacyHelper->setCtrl($this->_ctrl);
		$isAdmin = $privacyHelper->isEventAdmin($eid, $uid);
        return $isAdmin;
	}
	
	public function isMember($eid, $uid){
		
		Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/modules/event/helpers', 'Helper');
		$privacyHelper = $this->getOtherHelper('modelLoader');
		$privacyHelper->setCtrl($this->_ctrl);
		$isMember = $privacyHelper->isEventMember($eid, $uid);
        return $isMember;
	}
	
	public function isCanAddBookmark($eid, $uid){
		return $this->isCanAdd('bookmark', $eid, $uid);
	}
	
	public function isCanAddForum($eid, $uid){
		return $this->isCanAdd('forum', $eid, $uid);
	}
	
	public function isEnableBookmark($eid){
		return $this->isEnable('bookmark', $eid);
	}
	
	public function isEnableForum($eid){
		return $this->isEnable('forum', $eid);
	}
	
	public function isCanRemoveBookmark($eid, $uid){
		$eid = (int)$eid;
		$uid = (int)$uid;
		if(!$eid || !$uid)
			return false;
		else{
			return true;
		}
	}
}

